/* Copyright */
/*******************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Tsunagi-BPM
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *******************************************************************************/
 /* Copyright End */
 
package tsunagi.json.path;

import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class TestJsonPath {
	
	public static final void main(String[] args) throws Exception {
		String fn = "C:/app/eclipse/workspace/myDev/J-Path/testData/rest-like.json";
//		String fn = "C:/app/eclipse/workspace/myDev/J-Path/testData/all.txt";
		String content = getCodeLinesInString(getContents(fn));
		JsonPath jp = new JsonPath(content);
//		String path = "/obj1/array1/array2/prop1";
//		String path = "/obj1/array1/obj1/prop2";
//		String path = "/obj1/array1/obj1";
//		String path = "/obj1/array1/prop1";
//		String path = "/array1/obj1/prop2";
//		String path = "/array1/obj1";
//		String path = "/array1/prop1";
//		String path = "/array1";
//		String path = "/obj1/obj2/prop2";
//		String path = "/obj1/obj2";
//		String path = "/obj1/prop1";
//		String path = "/prop1";
//		String path = "/obj1/array1/obj1['prop1' = 'val 31' & 'prop2' = 'val 12']";
//		String path = "/resources['path'='/rs/obj1']/actions['action'='get']/requestAnalyzer";
//		String path = "/resources['path'='/rs/obj1']/actions['action'='get']";
//		String path = "/resources['name'='obj1']/actions['action'='get']";
//		String path = "/resources/path";
//		String path = "/resources/actions/requestProcessor/dataClass";
		String path = "/resources['path'='/rs/obj1']/actions['action'='get']/requestProcessor['dataClass'='Result2']";
		
		try {
			Object result = jp.get(path);
			System.out.println("[" + result + "]");
		} catch(JsonPathException e) {
			System.out.println(e.getMessage());
		}
	}
	
	public static List<String> getContents(String fname) throws Exception {
		Path path = FileSystems.getDefault().getPath(fname);
		return Files.readAllLines(path, StandardCharsets.UTF_8);
	}
	
	public static String getCodeLinesInString(List<String> codeLines) {
		StringBuffer result = new StringBuffer();
		for(String line: codeLines) {
			result.append(line);
		}
		return result.toString();
	}
}
